import { TreeNode } from "./00_二叉搜索树BSTree";

class AVLTreeNode<T> extends TreeNode<T> {
  left: AVLTreeNode<T> | null = null;
  right: AVLTreeNode<T> | null = null;
  parent: TreeNode<T> | null = null;
  // 当前节点的高度
  height: number = 1;
  // 获取节点最大高度
  private getMaxHeight(): number {
    const leftHeight = this.left ? this.left.getMaxHeight() : 0;
    const rightHeight = this.right ? this.right.getMaxHeight() : 0;

    return Math.max(leftHeight, rightHeight) + 1;
  }

  // 获取平衡因子
  private getBalanceFactor(): number {
    const leftHeight = this.left ? this.left.getMaxHeight() : 0;
    const rightHeight = this.right ? this.right.getMaxHeight() : 0;

    return leftHeight - rightHeight;
  }

  // 判断是否平衡
  get isBalanced(): boolean {
    // -1 0 1就返回true
    return Math.abs(this.getBalanceFactor()) <= 1; // 平衡因子绝对值小于等于1
  }

  // 判断更高子节点
  get getHigherChild(): AVLTreeNode<T> | null {
    const leftHeight = this.left ? this.left.getMaxHeight() : 0;
    const rightHeight = this.right ? this.right.getMaxHeight() : 0;

    if (leftHeight > rightHeight) return this.left;
    if (leftHeight < rightHeight) return this.right;
    return this.isLeft ? this.left : this.right;
  }
}

const root = new AVLTreeNode(10);
root.right = new AVLTreeNode(15);
root.right.right = new AVLTreeNode(20);
// console.log(root.isBalanced);
// console.log(root.right.isBalanced);
console.log(root.getHigherChild);

export {};
